#!/bin/bash
#############################################################################
#  Copyright (C) 2013-2015 Lawrence Livermore National Security, LLC.
#  Produced at Lawrence Livermore National Laboratory (cf, DISCLAIMER).
#  Written by Albert Chu <chu11@llnl.gov>
#  LLNL-CODE-644248
#
#  This file is part of Magpie, scripts for running Hadoop on
#  traditional HPC systems.  For details, see https://github.com/llnl/magpie.
#
#  Magpie is free software; you can redistribute it and/or modify it
#  under the terms of the GNU General Public License as published by
#  the Free Software Foundation; either version 2 of the License, or
#  (at your option) any later version.
#
#  Magpie is distributed in the hope that it will be useful, but
#  WITHOUT ANY WARRANTY; without even the implied warranty of
#  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
#  General Public License for more details.
#
#  You should have received a copy of the GNU General Public License
#  along with Magpie.  If not, see <http://www.gnu.org/licenses/>.
#############################################################################

# This script sets up configuration files for jobs.  For the most
# part, it shouldn't be editted.  See job submission files for
# configuration details.

if [ "${ZOOKEEPER_SETUP}" != "yes" ]
then
    exit 0
fi

source ${MAGPIE_SCRIPTS_HOME}/magpie/exports/magpie-exports-submission-type
source ${MAGPIE_SCRIPTS_HOME}/magpie/exports/magpie-exports-dirs
source ${MAGPIE_SCRIPTS_HOME}/magpie/lib/magpie-lib-defaults
source ${MAGPIE_SCRIPTS_HOME}/magpie/lib/magpie-lib-node-identification
source ${MAGPIE_SCRIPTS_HOME}/magpie/lib/magpie-lib-setup
source ${MAGPIE_SCRIPTS_HOME}/magpie/lib/magpie-lib-helper

zookeeperjavahomesubst=`echo "${JAVA_HOME}" | sed "s/\\//\\\\\\\\\//g"`
zookeeperlogdirsubst=`echo "${ZOOKEEPER_LOG_DIR}" | sed "s/\\//\\\\\\\\\//g"`
zookeeperconfdirsubst=`echo "${ZOOKEEPER_CONF_DIR}" | sed "s/\\//\\\\\\\\\//g"`
zookeeperhomesubst=`echo "${ZOOKEEPER_HOME}" | sed "s/\\//\\\\\\\\\//g"`
magpiescriptshomesubst=`echo "${MAGPIE_SCRIPTS_HOME}" | sed "s/\\//\\\\\\\\\//g"`

# Zookeeper master is only for setup/teardown management, not for actual Zookeeper
# We need this environment file just for the setup/teardown scripts
if Magpie_am_I_master
then
    Magpie_find_conffile "Zookeeper" ${ZOOKEEPER_CONF_FILES:-""} "zookeeper-master-env.sh" "pre_zookeepermasterenvsh"

    post_zookeepermasterenvsh=${ZOOKEEPER_CONF_DIR}/zookeeper-master-env.sh

    cp ${pre_zookeepermasterenvsh} ${post_zookeepermasterenvsh}

    sed -i \
        -e "s/MAGPIEJAVAHOME/${zookeeperjavahomesubst}/g" \
        -e "s/ZOOKEEPERLOGDIR/${zookeeperlogdirsubst}/g" \
        -e "s/ZOOKEEPERCONFDIR/${zookeeperconfdirsubst}/g" \
        -e "s/ZOOKEEPERHOME/${zookeeperhomesubst}/g" \
        -e "s/MAGPIESCRIPTSHOME/${magpiescriptshomesubst}/g" \
        ${post_zookeepermasterenvsh}

    if [ "${MAGPIE_REMOTE_CMD:-ssh}" != "ssh" ]
    then
        echo "export ZOOKEEPER_SSH_CMD=\"${MAGPIE_REMOTE_CMD}\"" >> ${post_zookeepermasterenvsh}
    fi
    if [ "${MAGPIE_REMOTE_CMD_OPTS}X" != "X" ]
    then
        echo "export ZOOKEEPER_SSH_OPTS=\"${MAGPIE_REMOTE_CMD_OPTS}\"" >> ${post_zookeepermasterenvsh}
    fi
fi

# zookeepernoderank set if succeed and it's a Zookeeper worker
if ! Magpie_am_I_a_zookeeper_node
then
    exit 0
fi

# For rest of setup, we will use cluster specific paths
Magpie_make_all_local_dirs_node_specific

extrajvmflags=""

# Setup node directory per node
if [ "${MAGPIE_ONE_TIME_RUN}" == "yes" ]
then
    zookeeperdatadirpath="${ZOOKEEPER_DATA_DIR}/${MAGPIE_JOB_NAME}/${MAGPIE_JOB_ID}/node-${zookeepernoderank}"
else
    zookeeperdatadirpath="${ZOOKEEPER_DATA_DIR}/node-${zookeepernoderank}"
fi
zookeeperdatadirpathsubst=`echo "${zookeeperdatadirpath}" | sed "s/\\//\\\\\\\\\//g"`

mkdir -p ${zookeeperdatadirpath}
if [ $? -ne 0 ] ; then
    echo "mkdir failed making ${zookeeperdatadirpath}"
    exit 1
fi

echo "${zookeepernoderank}" > ${zookeeperdatadirpath}/myid

if [ "${ZOOKEEPER_TICKTIME}X" != "X" ]
then
    zookeeperticktime=${ZOOKEEPER_TICKTIME}
else
    zookeeperticktime=2000
fi

if [ "${ZOOKEEPER_INITLIMIT}X" != "X" ]
then
    zookeeperinitlimit=${ZOOKEEPER_INITLIMIT}
else
    if [ "${ZOOKEEPER_DATA_DIR_TYPE}" == "local" ]
    then
        zookeeperinitlimit=10
    else
        zookeeperinitlimit=20
    fi
fi

if [ "${ZOOKEEPER_SYNCLIMIT}X" != "X" ]
then
    zookeepersynclimit=${ZOOKEEPER_SYNCLIMIT}
else
    if [ "${ZOOKEEPER_DATA_DIR_TYPE}" == "local" ]
    then
        zookeepersynclimit=5
    else
        zookeepersynclimit=10
    fi
fi

# set java.io.tmpdir
extrajvmflags="${extrajvmflags}${extrajvmflags:+" "}-Djava.io.tmpdir=${ZOOKEEPER_LOCAL_SCRATCHSPACE_DIR}/tmp"

if [ ! -d "${ZOOKEEPER_LOCAL_SCRATCHSPACE_DIR}/tmp" ]
then
    mkdir -p ${ZOOKEEPER_LOCAL_SCRATCHSPACE_DIR}/tmp
    if [ $? -ne 0 ] ; then
        echo "mkdir failed making ${ZOOKEEPER_LOCAL_SCRATCHSPACE_DIR}/tmp"
        exit 1
    fi
fi

# disable hsperfdata if using NO_LOCAL_DIR
if [ "${MAGPIE_NO_LOCAL_DIR}" == "yes" ]
then
    extrajvmflags="${extrajvmflags}${extrajvmflags:+" "}-XX:-UsePerfData"
fi

#
# Get config files for setup
#

# Magpie_find_conffile will set the 'pre' filenames
Magpie_find_conffile "Zookeeper" ${ZOOKEEPER_CONF_FILES:-""} "zoo.cfg" "pre_zookeepercfg"
Magpie_find_conffile "Zookeeper" ${ZOOKEEPER_CONF_FILES:-""} "zookeeper-env.sh" "pre_zookeeperenvsh"
Magpie_find_conffile "Zookeeper" ${ZOOKEEPER_CONF_FILES:-""} "zookeeper.log4j.properties" "pre_log4jproperties"

post_zookeepercfg=${ZOOKEEPER_CONF_DIR}/zoo.cfg
post_zookeeperenvsh=${ZOOKEEPER_CONF_DIR}/zookeeper-env.sh
post_log4jproperties=${ZOOKEEPER_CONF_DIR}/log4j.properties

#
# Setup Zookeeper configuration files and environment files
#

cp ${pre_zookeepercfg} ${post_zookeepercfg}

Magpie_get_magpie_hostname
ZOOKEEPER_THIS_HOST=${magpie_hostname}

sed -i \
    -e "s/ZOOKEEPERDATADIR/${zookeeperdatadirpathsubst}/g" \
    -e "s/ZOOKEEPERTICKTIME/${zookeeperticktime}/g" \
    -e "s/ZOOKEEPERINITLIMIT/${zookeeperinitlimit}/g" \
    -e "s/ZOOKEEPERSYNCLIMIT/${zookeepersynclimit}/g" \
    -e "s/ZOOKEEPERCLIENTPORT/${default_zookeeper_client_port}/g" \
    -e "s/ZOOKEEPERHOST/${ZOOKEEPER_THIS_HOST}/g" \
    ${post_zookeepercfg}

zookeepernodes=`cat ${ZOOKEEPER_CONF_DIR}/workers`

servercount=1
for zookeepernode in ${zookeepernodes}
do
    echo "server.${servercount}=${zookeepernode}:${default_zookeeper_peer_port}:${default_zookeeper_leader_port}" >> ${post_zookeepercfg}
    servercount=`expr ${servercount} + 1`
done

extrajvmflagssubst=`echo "${extrajvmflags}" | sed "s/\\//\\\\\\\\\//g"`

cp ${pre_zookeeperenvsh} ${post_zookeeperenvsh}

sed -i \
    -e "s/MAGPIEJAVAHOME/${zookeeperjavahomesubst}/g" \
    -e "s/ZOOKEEPERLOGDIR/${zookeeperlogdirsubst}/g" \
    -e "s/ZOOKEEPERCONFDIR/${zookeeperconfdirsubst}/g" \
    -e "s/ZOOKEEPERHOME/${zookeeperhomesubst}/g" \
    -e "s/ZOOKEEPERJVMFLAGS/${extrajvmflagssubst}/g" \
    ${post_zookeeperenvsh}

cp ${pre_log4jproperties} ${post_log4jproperties}

exit 0
